import math
from libavg import avg
from trash import Trash

# this trash object is destroyable by a pinch apart move
class PinchableTrash(Trash):

    def __init__(self, posi, par, weight, img): 
        Trash.__init__(self, posi, par, weight, img); # KONSTRUKTOR DER OBERKLASSE TRASH
        # FLAGS
        self.bool = False 
        self.__FstCaptureHolder = None 
        self.__SndCaptureHolder = None
        self.__FstFinger = None
        self.__SndFinger = None
        # EVENTS VERWALTEN
        self.setEventHandler(avg.CURSORDOWN, avg.TOUCH, self._enter) 
        self.setEventHandler(avg.CURSORMOTION, avg.TOUCH, self._capture)
        self.setEventHandler(avg.CURSORUP, avg.TOUCH, self._leave) 

    def _enter(self, event):
        self.__updateable = False 
        if self.anim is not None:
            self.anim.abort() # BRECHE LAUFENDE ANIMATION AB
        #assign CaptureHolders
        if self.__FstCaptureHolder is None:
            self.__FstCaptureHolder = event.cursorid
            self.__FstFinger = event
            event.node.setEventCapture(event.cursorid)
        elif self.__SndCaptureHolder is None:
            #if both fingers trigger __startPinch, save their initial distance
            self.__initialDistance = self.getDistance(self.__FstFinger.pos, event.lastdownpos) 
            self.__SndCaptureHolder = event.cursorid
            self.__SndFinger = event
            event.node.setEventCapture(event.cursorid)  
            
    def _capture(self, event):        
        #if first finger moves and second finger holds:
        if event.cursorid == self.__FstCaptureHolder and self.__SndCaptureHolder != None:
            #compare old distance with new distance
            newDistance = self.getDistance(self.__SndFinger.pos, event.pos)
            if newDistance > self.__initialDistance+self.width//3:
                self.bool = True
        #if second finger moves and first finger holds:       
        elif event.cursorid == self.__SndCaptureHolder and self.__FstCaptureHolder != None:
            newDistance = self.getDistance(self.__FstFinger.pos, event.pos)
            if newDistance > self.__initialDistance+self.width//3:
                self.bool = True            
        #else update the event references (if only one finger is moved, e.g.):
        else:
            if event.cursorid == self.__FstCaptureHolder:
                self.__FstFinger = event
            elif event.cursorid == self.__SndCaptureHolder:
                self.__SndFinger = event
        
    def _leave(self, event):
        self.__updateable = True
        #release events, null the references
        if event.cursorid == self.__FstCaptureHolder:
            event.node.releaseEventCapture(event.cursorid)
            self.__FstCaptureHolder = None
            self.__FstFinger = None
        if event.cursorid == self.__SndCaptureHolder:
            event.node.releaseEventCapture(event.cursorid)
            self.__SndCaptureHolder = None
            self.__SndFinger = None
        if self.bool:
            self.fadeOut()
        else:
            self.fallDown(self.wind)